package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.search.pojo.SkuInfo;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SearchResultMapperImpl implements SearchResultMapper {
    @Override
    public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {

        List<T> content = new ArrayList<>();
        // 如果没有结果返回空
        if (searchResponse.getHits() == null || searchResponse.getHits().getTotalHits() <= 0) {
            return new AggregatedPageImpl<>(content);
        }
        for (SearchHit searchHit : searchResponse.getHits()) {
            String sourceAsString = searchHit.getSourceAsString();
            SkuInfo skuInfo = JSON.parseObject(sourceAsString, SkuInfo.class);

            Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
            if (highlightFields != null
                    && highlightFields.size() > 0
                    && highlightFields.get("name") != null
                    && highlightFields.get("name").getFragments() != null) {

                HighlightField highlightField = highlightFields.get("name");

                // 有高亮则设置高亮的值
                if (highlightField != null) {
                    StringBuffer stringBuffer = new StringBuffer();
                    for (Text text : highlightField.getFragments()) {
                        stringBuffer.append(text.string());
                    }
                    if (stringBuffer != null) {
                        skuInfo.setName(stringBuffer.toString());
                    }
                }
                content.add((T) skuInfo);
            }

        }
        return new AggregatedPageImpl<>(content, pageable, searchResponse.getHits().getTotalHits(),
                searchResponse.getAggregations(), searchResponse.getScrollId());
    }
}
